package org.alex.samples.dao;

import java.text.SimpleDateFormat;
import java.util.Iterator;

import org.alex.samples.model.ExamScore;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class ISoreDaoImpl extends BaseHibernateDAO<ExamScore> implements IScoreDao {

	@Override
	public Iterator<ExamScore> iterate(String queryString, Object[] values) {
		return null;
	}

	@Override
	public ExamScore getScoreById(Integer id) {
		String hql = "from ExamScore where id=" + id;
		return (ExamScore) createQuery(hql).uniqueResult();
	}

	@Override
	public void createScore(ExamScore score) throws Exception {
		save(score);
	}

	@Override
	public void addScore(ExamScore score) throws Exception {
		String sql = "insert into exam_score (user_id,subject_id,exam_time,score) values ('" + score.getUserId() + "'," + score.getSubjectId() + ",'"
				+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(score.getExamTime()) + "'," + score.getScore() + ")";
		this.createSqlQuery(sql).executeUpdate();
	}

	@Override
	public ExamScore getScoreByUserIdAndSubId(String userId, Integer subId) throws Exception {
		String sql ="from ExamScore where userId='"+userId+"' and subjectId="+subId;
		return (ExamScore) createQuery(sql).uniqueResult();
	}
	
}
